package com.power.task;

import com.power.DlykServerApplication;
import com.power.enu.DicEnum;
import com.power.model.TDicType;
import com.power.model.TDicValue;
import com.power.model.TProduct;
import com.power.service.DicTypeService;
import com.power.service.ProductService;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@EnableScheduling //开启定时任务
@Component
public class DicTypeDataTask {
    @Resource
    private DicTypeService dicTypeService;
    @Resource
    private ProductService productService;

    //    @Scheduled(cron = "${project.task.cron}", zone = "Asia/Shanghai", timeUnit = TimeUnit.MILLISECONDS)
    //启动完成过1秒钟执行一次，后续每隔5分钟执行一次
    @Scheduled(fixedDelayString = "${project.task.delay}", zone = "Asia/Shanghai", timeUnit = TimeUnit.MILLISECONDS, initialDelay = 1000)
    public void task() {
        List<TDicType> tDicTypeList = dicTypeService.loadAllDicData();
        tDicTypeList.forEach(item -> {
            String typeCode = item.getTypeCode();
            List<TDicValue> dicValueList = item.getDicValueList();
            DlykServerApplication.cacheMap.put(typeCode, dicValueList);
        });
        List<TProduct> productList = productService.getProductList();
        DlykServerApplication.cacheMap.put(DicEnum.PRODUCT.getCode(), productList);
    }
}
